//
//  CustomCalloutView.m
//  Category_demo2D
//
//  Created by xiaoming han on 13-5-22.
//  Copyright (c) 2013年 songjian. All rights reserved.
//

#import "CustomCalloutView.h"
#import <QuartzCore/QuartzCore.h>

#define kArrorHeight    5

@implementation CustomCalloutView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        [self initSubViews];
    }
    return self;
}
- (void)initSubViews{
    //添加时间
    self.timeStatusLabel = [[UILabel alloc]init];
    
    self.timeStatusLabel.font = [UIFont boldSystemFontOfSize:12];
    
    self.timeStatusLabel.textColor = WHITE_COLOR;
    
    [self addSubview:self.timeStatusLabel];
    
    
    // 添加分割线
    self.separateImageView = [[UIImageView alloc]init];
    [self addSubview:self.separateImageView];
    
    
    //添加地点
    self.locationLabel = [[UILabel alloc]init];
    
    self.locationLabel.font = [UIFont boldSystemFontOfSize:12];
    
    self.locationLabel.textColor = BLUE_COLOR;
    
    [self addSubview:self.locationLabel];
    
    //添加上下车人数
    self.numberLabel = [[UILabel alloc]init];
    
    self.numberLabel.font = [UIFont boldSystemFontOfSize:12];
    
    self.numberLabel.textColor = WHITE_COLOR;
    
    [self addSubview:self.numberLabel];
    
    //屏幕适配
    [self fitUI];
}
-(void)fitUI{
    [self.timeStatusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(14));
        make.width.equalTo(@(32));
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.mas_left).offset(15);
    }];
    [self.separateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeStatusLabel.mas_right).offset(5);
        make.width.equalTo(@(1));
        make.centerY.equalTo(self.mas_centerY);
        make.height.equalTo(@(self.height - 10));
    }];
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(15);
        make.left.equalTo(self.separateImageView.mas_right).offset(5);
        make.height.equalTo(@(14));
        make.right.equalTo(self.mas_right).offset(15);
    }];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.locationLabel.mas_bottom).offset(5);
        make.left.equalTo(self.separateImageView.mas_right).offset(5);
        make.height.equalTo(@(14));
        make.right.equalTo(self.mas_right).offset(15);
    }];
}
#pragma mark - draw rect

- (void)drawRect:(CGRect)rect
{
    
    [self drawInContext:UIGraphicsGetCurrentContext()];
    
    self.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.layer.shadowOpacity = 0.5;
    self.layer.shadowOffset = CGSizeMake(0.65, 0.65);
    self.layer.shadowRadius = 0.5;
    
}

- (void)drawInContext:(CGContextRef)context
{
    
    CGContextSetLineWidth(context, 1.0);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    
    [self getDrawPath:context];
    CGContextFillPath(context);
    
}

- (void)getDrawPath:(CGContextRef)context
{
    CGRect rrect = self.bounds;
    CGFloat radius = 2.5;
    CGFloat minx = CGRectGetMinX(rrect),
    midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-kArrorHeight;
    
    CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
    CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
    CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
    
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextClosePath(context);
}
@end
